﻿CREATE PROCEDURE [dbo].[usp_insert_ContextProperty]
(
    @MessageID				uniqueidentifier 
    ,@Name					varchar(256) 
    ,@Value					varchar(4096) 
    ,@Type					varchar(256) 
	,@Debug					bit			= 1
)
AS

/****************************************************************************
Name: usp_insert_ContextProperty.sql
Description: Inserts a message's context property into the ContextProperty
	table.
*****************************************************************************/
SET NOCOUNT ON;

-- Bootstrap
DECLARE @DebugPrefix       varchar(10);
DECLARE @ErrorCode         int;

SET @ErrorCode          = 0;
SET @DebugPrefix        = '>>> DEBUG: ';


-- Test harness
/*
DECLARE @ContextPropertyID		uniqueidentifier 
DECLARE @MessageID				uniqueidentifier 
DECLARE @Name					varchar(50) 
DECLARE @Value					varchar(50) 
DECLARE @Type					varchar(50) 
DECLARE @InsertedDate			datetime 
DECLARE @InsertedBy				varchar(50) 
DECLARE @ModifiedDate			datetime	= NULL
DECLARE @ModifiedBy				varchar(50) = NULL
		@Debug					bit			= 1
SELECT
	 @MessageID			= NEWID()		
	,@FaultID			= NEWID()		
	,@MessageName		= 'test'	
	,@RoutingUrl		= 'test_url'	
	,@MessageData	
	,@InsertedDate	
	,@InsertedBy	
	,@ModifiedDate	
	,@ModifiedBy
	,@Debug                 = 1;

*/

-- Print parameters
/*IF (@Debug > 0)
BEGIN
	PRINT @DebugPrefix + '@Name = ' + @Name;
	PRINT @DebugPrefix + '@Value = ' + @Value;
	PRINT @DebugPrefix + '@Type = ' + @Type;
	--PRINT @DebugPrefix + '@ContextPropertyID = ' + CAST(@ContextPropertyID AS varchar);
    PRINT @DebugPrefix + '@MessageID = ' + CAST(@MessageID AS varchar);
END;*/


INSERT INTO 
			[dbo].[ContextProperty]
			(
				[MessageID]
				,[Name]
				,[Value]
				,[Type]
			)

VALUES
			(
				 @MessageID			
				,@Name				
				,@Value				
				,@Type			
			)	

-- CHECK FOR ERROR
SET @ErrorCode = @@Error;

IF (@ErrorCode <> 0) GOTO ERROR_HANDLER;

RETURN 0;

-- Handles errors.
ERROR_HANDLER:
IF (@ErrorCode <> 0)
BEGIN
    IF (@Debug > 0) 
		BEGIN
			PRINT @DebugPrefix + 'Error Code = ' + CAST(@ErrorCode AS varchar(10)) + ' occurred.';
			RETURN @ErrorCode
		END

    -- Don't return any evidence of what specific error occurred, i.e. 
    -- we want to ward off potential hackers.
    RETURN 1;
END;

SET NOCOUNT OFF;
